import type { RouteLocationNormalized, RouteRecordNameGeneric } from 'vue-router'
import { nextTick } from 'vue'
import { useTitle } from '@vueuse/core'
import router from "@/router"
import logger from './logger'

/**
 * 判断字符串是否为空
 * @param str 检测字符串
 * @returns 
 */
export const isNotBlank = (str: string | null) => str != undefined && str != null && str.length != 0

/**
 * 设置网页标题
 * @param route 路由
 */
export const setWebTitle = ((route: RouteLocationNormalized) => {
    nextTick(() => {
        const title = route.meta && typeof route.meta.title === 'string' ? route.meta.title : null
        useTitle(title)
    })
})

/**
 * 检测路由是否存在
 * @param name 路由名称
 */
export const checkRouteException = ((name: RouteRecordNameGeneric) => {
    if (!router.getRoutes().some(route => route.name === name)) router.replace("/404")
})

export const resize = () => {
    setHtmlFontSize()
}

export const setHtmlFontSize = () => {
    // const htmlDom = document.getElementsByTagName('html')[0];
    // let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth;
    // if (htmlWidth >= 750) {
    //     htmlWidth = 750;
    // } else if (htmlWidth <= 430 && htmlWidth > 375) {
    //     htmlWidth = 525;
    // } else if (htmlWidth <= 375) {
    //     htmlWidth = 450;
    // }
    // htmlDom.style.fontSize = `${htmlWidth / 75}px`;
    // logger.info(htmlWidth, `${htmlWidth / 75}px`)
};

export const isMobile = () => {
    const userAgent = navigator.userAgent.toLowerCase();
    const mobileKeywords = [
        'android', 'iphone', 'ipad', 'ipod', 'windows phone', 'blackberry', 'webos'
    ];
    const isMobileUserAgent = mobileKeywords.some(keyword => userAgent.includes(keyword));
    const isMobileScreen = window.innerWidth <= 768;
    const isMobileOrientation = typeof window.orientation !== 'undefined';
    return isMobileUserAgent || isMobileScreen || isMobileOrientation;
}




